$(function () {
    // 授权角色关闭dialog
    $('#closeId2').click(function () {
        $("#dialogId2").dialog("close");
    });

    //编辑角色
    $("#saveId2").click(function () {
        // method
        saveUserRole();
    });

});

/**
 * 用户授权角色-first
 */
function authRole(){
    // open dialog dg
    openRoleDialog();

}

/**
 * open role dialog
 */
function openRoleDialog(){
    var selRow = $("#dg").datagrid("getSelections");// 返回选中多行
    if (selRow.length == 0) {
        $.messager.alert("提示", "请选择一行数据！", "warning");
        return;
    }
    var userId = selRow[0].id;

    // set user val
    $('#userId').val(userId);
    // getRole
    getRole(userId);
}

/**
 * get all role
 */
function getRole(userId) {
    var menus = [];
    $.ajaxSync($WEB_ROOT_PATH + '/role/getAllRole', 'post', null, function (res) {
        if (res.code === 0) {
            menus = res.data;
        } else {
            $.messager.alert("提示", "菜单异常", "", function () {
                //window.location.href = $WEB_ROOT_PATH + "/";
            });
        }
    }, function (res) {
    }, 'json');

    $("#roleTreeId").tree({ //tree菜单全部加载
        data: menus,
        lines: false,
        animate: false,
        checkbox: true,
        cascadeCheck: false, //不级联选择
        onLoadSuccess: function (node, data) {
            if (data) {
                $("#dialogId2").dialog({
                    closed: false,
                    closable: false,
                    /*left: 0,
                    top: 0,*/
                    title: '授权角色',
                    iconCls: 'icon-save',
                    onOpen: function () {
                        // tree 回显
                        $.ajaxAsync($WEB_ROOT_PATH + '/role/getRoleByUserId', 'GET', {userId: userId}, function (res) {
                            if (res.code === 0) {
                                $(res.data).each(function(index,data){
                                    var node = $('#roleTreeId').tree('find', data.id);
                                    $('#roleTreeId').tree('check', node.target);
                                });
                            } else {
                                resError(res);
                            }
                        }, function (res) {
                        }, 'json');
                    }
                });
            }
        },
        onClick: function (node) { //
        }
    });
}

/**
 * 用户授权角色
 */
function saveUserRole(){
    var userId = $('#userId').val();
    var node = $('#roleTreeId').tree('getChecked');
    /*if (node.length == 0) {
        $.messager.alert('提示', '请至少选一个节点', "info");
        return;
    }*/
    var roleIds = [];
    for (var i = 0; i < node.length; i++) {
        if (null != node[i].id) {
            roleIds.push(node[i].id);
        }
    }
    if(roleIds.length==0){
        // delete flag
        roleIds.push("-1");
    }
    $.ajaxAsync($WEB_ROOT_PATH + '/user/authRole', 'post', {roleIds:roleIds,userId:userId}, function (res) {
        if (res.code === 0) {
            $.messager.show({
                title: '提示',
                msg: res.data,
                timeout: 3000,
                showType: 'slide'
            });
        } else {
            resError(res);
        }
    }, function (res) {
    }, 'json');
}

/**
 * ajax req error
 * @param res
 */
function resError(res) {
    $.messager.alert('提示', res.data, "", function () {
    });
    $('.panel-tool-close').hide();
}